package com.wyr.common.exception;

import cn.dev33.satoken.exception.NotLoginException;
import com.wyr.common.enums.HttpCodeEnum;
import com.wyr.common.vo.Result;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常信息处理类
 * 注意：
 *
 * @author wangyiren
 * @version 1.0
 * &#064;title GlobalException
 * &#064;description
 * &#064;date 2025/03/07 09:12
 * @Hidden springdoc-openapijava 库有助于使用 spring boot 项目自动生成 API 文档。
 * springdoc-openapi通过在运行时检查应用程序来根据 spring 配置、类结构和各种注释推断 API 语义
 */
@Slf4j
@Hidden
@RestControllerAdvice
public class GlobalException {

    /**
     * 处理业务异常
     *
     * @param e BusinessException
     * @return businessException
     */
    @ExceptionHandler(value = BusinessException.class)
    public Result<?> businessException(BusinessException e) {
        log.error("业务异常 => code: {}, 原因是: {}", e.getCode(), e.getMessage());
        return Result.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理未知(600)异常
     *
     * @param e otherException
     * @return globalException
     */
    @ExceptionHandler(value = Exception.class)
    public Result<?> globalException(Exception e) {
        log.error("未知(600)异常 => 原因是: {}", e.getMessage());
        return Result.error(HttpCodeEnum.RC600.getCode(), HttpCodeEnum.RC600.getMessage());
    }

    /**
     * SaToken 未登录异常
     *
     * @param e otherException
     * @return globalException
     */
    @ExceptionHandler(value = NotLoginException.class)
    public Result<?> notLoginException(Exception e) {
        log.error("saToken异常 => 原因是: {}", e.getMessage());
        return Result.error(HttpCodeEnum.RC101.getCode(), HttpCodeEnum.RC101.getMessage());
    }

    /**
     * 处理页面不存在(404)异常
     *
     * @param e HttpServletRequest
     * @return noHandlerFoundException
     */
    @ExceptionHandler(value = NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result<?> noHandlerFoundException(HttpServletRequest e) {
        log.error("请求地址错误(404)异常 => 请求方式: {}, 请求地址: {}", e.getMethod(), e.getServletPath());
        return Result.error(HttpCodeEnum.RC404.getCode(), HttpCodeEnum.RC404.getMessage());
    }

    /**
     * 处理请求方式错误(405)异常
     *
     * @param e HttpServletRequest
     * @return httpRequestMethodNotSupportedException
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public Result<?> httpRequestMethodNotSupportedException(HttpServletRequest e) {
        log.error("请求方式错误(405)异常 => 请求方式: {}, 请求地址: {}", e.getMethod(), e.getServletPath());
        return Result.error(HttpCodeEnum.RC405.getCode(), HttpCodeEnum.RC405.getMessage());
    }

}
